package gdonly.top.signin.utils;

import android.content.Context;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.util.Log;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;

/**
 * Created by zhuzhuxia on 2017/4/27.
 * ap:热点
 * wifi 与 ap 不能同时开启
 */
public class WifiUtil {


    private static WifiManager wifiManager;
    private final static int PWD_SUB_BIT_START = 4;
    private final static int PWD_SUB_BIT_STOP = 13;
    private final static String DEFAULT_WIFI_SSID = "signin";
    //private static String

    public static Boolean openApAndChangeSSID(Context context, String SSID){

        wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        wifiManager.setWifiEnabled(false);//close wifi
        try {
            WifiConfiguration apConfig = new WifiConfiguration();
            apConfig.SSID = SSID;


            apConfig.networkId = 1;
            apConfig.allowedAuthAlgorithms.clear();
            apConfig.allowedGroupCiphers.clear();
            apConfig.allowedKeyManagement.clear();
            apConfig.allowedPairwiseCiphers.clear();
            apConfig.allowedProtocols.clear();

            apConfig.preSharedKey= String.valueOf(
                    System.currentTimeMillis()).substring(PWD_SUB_BIT_START, PWD_SUB_BIT_STOP);
            Log.e("TAG", String.valueOf(
                    System.currentTimeMillis()).substring(PWD_SUB_BIT_START, PWD_SUB_BIT_STOP));

            apConfig.hiddenSSID = true;
            apConfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
            apConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
            apConfig.allowedKeyManagement.set(4);
            //apConfig.allowedKeyManagement.set(4);  
            apConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
            apConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
            apConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
            apConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);

            Method method = wifiManager.getClass().getMethod("setWifiApConfiguration", apConfig.getClass());
            method.invoke(wifiManager, apConfig);
            Method method1 = wifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, Boolean.TYPE);
            //返回热点打开状态
            return (Boolean) method1.invoke(wifiManager, null, true);
        }
        catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
        catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return true;
    }

    /**
     * 只负责关闭由openApAndChangeSSID开启的Ap
     * @return 操作结果
     */
    public static boolean closeAp(){

        if(wifiManager == null){
            return false;
        }
        try {
            WifiConfiguration apConfig = new WifiConfiguration();
            apConfig.SSID = DEFAULT_WIFI_SSID;
            apConfig.preSharedKey="xueba" + String.valueOf(
                    System.currentTimeMillis()).substring(PWD_SUB_BIT_START, PWD_SUB_BIT_STOP);

            Method method = wifiManager.getClass().getMethod(
                    "setWifiApEnabled", WifiConfiguration.class, Boolean.TYPE);
            return (Boolean) method.invoke(wifiManager, apConfig, false);//close ap
        }
        catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
        catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return false;
    }

    public static boolean includeAp( Context context, String ssid, String mac){

        wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);

        if(wifiManager.startScan()) {

            List<ScanResult> ssidlist = wifiManager.getScanResults();


            Log.e("TAG", ssidlist.size() + "size");
            for (ScanResult result : ssidlist) {


                Log.e("TAG", result.SSID+"  "+ssid);
                if (result.SSID.equals(ssid)) {

                    Log.e("TAG", "check in...");
                    return true;
                }
            }
        }
        return false;
    }
    public static String getMac(Context context){

        wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        Log.e("TAG",wifiManager.getConnectionInfo().getBSSID()+"mac\n "+wifiManager.getConnectionInfo().getMacAddress());
        return wifiManager.getConnectionInfo().getMacAddress();
    }
}
